import 'package:flutter/material.dart';
import 'package:shenling_project/LoginPage.dart';
import 'package:shenling_project/index.dart';
import 'package:shenling_project/mine/SettingPage.dart';
import 'package:shenling_project/utils/tokenManager.dart';

void main() {
  runApp(MaterialApp(
    routes: {
      '/': (BuildContext context) => const ShenLingIndex(),
      '/login': (BuildContext context) => const Loginpage(),
    },
    initialRoute: '/',
    //路由拦截
    onGenerateRoute: (settings) {
      String? token = TokenManager().getToken() ?? '';
      print(token);

      if (token.isEmpty && settings.name != '/login') {
        return MaterialPageRoute(builder: (context) => const Loginpage());
      }

      if (settings.name == '/setting') {
        return MaterialPageRoute(builder: (context) => const Settingpage());
      }

      return MaterialPageRoute(
        builder: (context) {
          return const ShenLingIndex();
        },
      );
    },
  ));
}
